home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / dev / lang / SML.lha / sml / Programs / functions chap 15 < prev   
Encoding:
Text File  |  1991-12-12  |  866 b   |  24 lines

  1. -fun map f [] = []
  2.   | map f (a::x) = (f a) :: (map f x)
  3. >val map = fn : ('a -> 'b) -> 'a list -> 'b list
  4.     (* the function map takes a function and a list as argument and *)
  5.     (* applies the function to all elements in the list.        *)
  6.     (* ie : -map square[1,3,5];  >[1,9,25]                *)
  7.  
  8. -fun twice f = f o f;
  9. >val twice = fn : ('a -> 'a) -> 'a -> 'a
  10.     (* the compose operator 'o' is a standard in ML and has lower    *)
  11.     (* precedence than any other standard operator            *)
  12.     (* ie : twice sqr 5 ->(sqr o sqr)5 ->sqr(sqr 5) ->sqr 25 ->625    *)
  13. other ie :
  14.     -fun last = hd o rev;
  15.     >val last = fn : 'a list -> 'a
  16.     
  17.     -fun second = hd o tl;
  18.     >val second = fn : 'a list -> 'a
  19.  
  20.     -fun third = hd o tl o tl;
  21.     >val third = fn : 'a list -> 'a
  22.         (* ie : third[1,3,5] -> (hd o tl o tl)[1,3,5] ->     *)
  23.         (*      (hd o tl)(tl[1,3,5]) -> (hd o tl)[3,5] ->    *)
  24.         (*      hd(tl[3,5]) -> hd[5] -> 5            *)